package com.smartsoft.fragment;


import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.smartsoft.R;
import com.smartsoft.model.ContactData;
import com.smartsoft.model.CountryItem;
import com.smartsoft.service.DataManager;


public class PreviewContactFragment extends Fragment {
    /**
     * Contains data submitted from edit page.
     */
    private ContactData contactData;
    private DataManager dataManager = DataManager.getInstance();
    /**
     * Callback handler for Back and Accept buttons
     */
    private OnAcceptOrBackListener mAcceptOrBackHandler;


    /**
     * Container (activity) which contains this fragment have to implements
     * interface in order to be notified when user presses Back or Accept buttons.
     */
    public interface OnAcceptOrBackListener {
        /**
         * This method will be called on the fragment's container (activity)
         * in case when user presses Accept button.
         *
         * @param contactData contact data which was accepted by the user
         */
        public void onContactAccepted(ContactData contactData);

        /**
         * This method will be called on the fragment's container (activity)
         * in case when user presses Back button and thereby rejected the
         * contact information.
         */
        public void onContactRejected();
    }


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (!(activity instanceof OnAcceptOrBackListener)) {
            throw new ClassCastException(activity + " have to implements OnAcceptOrBackListener interface");
        }
        mAcceptOrBackHandler = (OnAcceptOrBackListener) activity;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View previewFragmentView = inflater.inflate(R.layout.preview_contact_fragment, container, false);
        if (contactData != null) {
            // Set image for cover flow preview
            ImageView previewAvatar = (ImageView) previewFragmentView.findViewById(R.id.preview_avatar_img);
            int selectedAvatarPos = dataManager.getAvatarDrawableIds().get(contactData.getAvatarPosition());
            previewAvatar.setImageResource(selectedAvatarPos);

            // User name preview
            TextView previewName = (TextView) previewFragmentView.findViewById(R.id.preview_name_text);
            previewName.setText(contactData.getName());

            // Phone number preview
            TextView previewPhoneNumber = (TextView) previewFragmentView.findViewById(R.id.preview_phone_number_text);
            previewPhoneNumber.setText(contactData.getPhoneNumber());

            // Email address preview
            TextView previewEmail = (TextView) previewFragmentView.findViewById(R.id.preview_email_text);
            previewEmail.setText(contactData.getEmail());

            // Country preview
            CountryItem selectedCountry = dataManager.getCountriesData().get(contactData.getSelectedCountryPosition());
            ImageView previewCountryFlag = (ImageView) previewFragmentView.findViewById(R.id.country_flag_image);
            previewCountryFlag.setImageResource(selectedCountry.getCountryFlagResId());
            TextView previewCountryName = (TextView) previewFragmentView.findViewById(R.id.country_name);
            previewCountryName.setText(selectedCountry.getCountryName());

        }

        // Delegate Back and Accept handling to the activity
        Button backButton = (Button) previewFragmentView.findViewById(R.id.back_btn);
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAcceptOrBackHandler.onContactRejected();
            }
        });
        Button acceptButton = (Button) previewFragmentView.findViewById(R.id.accept_btn);
        acceptButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAcceptOrBackHandler.onContactAccepted(contactData);
            }
        });

        return previewFragmentView;
    }

    public void setContactData(ContactData contactData) {
        this.contactData = contactData;
    }

}